package com.heyqing.topic.BinaryTreePaths_0257;

import java.util.*;
import java.util.stream.Collectors;

/**
 * ClassName:BinaryTreePaths
 * Package:com.heyqing.topic.BinaryTreePaths_0257
 * Description:
 *
 * @Date:2024/9/25
 * @Author:Heyqing
 */
public class BinaryTreePaths {

    private Set<String> resultSet = new HashSet<>();
    private final String separate = "->";

    public List<String> binaryTreePaths(TreeNode root) {
        achieveFunc(root, "");
        return resultSet.stream().collect(Collectors.toList());
    }

    private void achieveFunc(TreeNode root, String currentStr) {
        StringBuilder sb = new StringBuilder(currentStr);
        if (Objects.isNull(root)) return;
        if (currentStr.equals("")) {
            sb.append(root.val);
        } else {
            sb.append(separate + root.val);
        }
        if (Objects.isNull(root.left) && Objects.isNull(root.right)) {
            resultSet.add(sb.toString());
        }
        achieveFunc(root.left, sb.toString());
        achieveFunc(root.right, sb.toString());
    }

}
